package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Result;
import com.itheima.pojo.Comment;
import com.itheima.service.CommentService;
import com.itheima.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户评论
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;


    /**
     * 评论添加方法
     *
     * @param commentVo 前端传的评论Vo实体类
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody CommentVo commentVo) {
        commentService.add(commentVo);
        return new Result("success", "添加成功");
    }

    /**
     * 条件查询评论列表
     *
     * @param commentVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result list(@RequestBody CommentVo commentVo) {
        PageInfo<Comment> pageInfo = commentService.list(commentVo);
        return new Result("success", pageInfo);
    }

    /**
     * 删除评论
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody CommentVo commentVo) {
        try {
            commentService.delete(commentVo);
            return new Result("success", "删除成功");
        } catch (Exception e) {
            return new Result("fail", "删除失败");
        }
    }

    /**
     * 批量删除评论
     *
     * @param commentVoList
     * @return
     */
    @PostMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody List<CommentVo> commentVoList) {
        try {
            commentService.deleteBatch(commentVoList);
            return new Result("success", "删除成功");
        } catch (Exception e) {
            return new Result("fail", "删除失败");
        }

    }

    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody CommentVo commentVo) {
        try {
            commentService.edit(commentVo);
            return new Result("success", "修改成功");
        } catch (Exception e) {
            return new Result("fail", "修改失败");
        }
    }
}
